package com.gable.common.util;

import java.math.BigDecimal;

//此类主要用于金钱转换
public class MoneyFormatUtil {
	
	// 将金钱类型转换成1000,000.00类型的字符串
	public static String MoneyToString(Long money) {
		if (null == money || money <= 0) {
			return "0";
		}
		String result = "";
		//如果金钱小于100，则为角分，直接返回小数
		if(money<100){
			BigDecimal big = new BigDecimal(money.intValue());
			double b =  big.divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
			result = b+"";
			return result;
		}
		String moneyStr = money + "";
		String branch = moneyStr.substring(moneyStr.length()-2);
		int length = moneyStr.length()-2;
		if (length > 3) {
			int divider = (length-1) / 3 + 1;
			int k = 0;
			for (int i = divider; i >0; i--) {
				if (i == 1) {
					result = moneyStr.substring(0, (length % 3 == 0 ? 3 : length % 3)) + "," + result;
				} else {
					result = moneyStr.substring((length-((k+1)*3)), (length-k*3))+ "," + result;
				}
				k++;
			}
			if (!result.equals("")) {
				result = result.substring(0, result.length() - 1);
			}
			
		}else{
			result = moneyStr.substring(0,moneyStr.length()-2);
		}
		result = result + "."+ branch;
		return result;
	}
	
}
